package com.scnu.hachimi.show;

import com.scnu.hachimi.manager.ObjectManager;
import com.scnu.hachimi.manager.ResourcesManager;
import com.scnu.hachimi.tool.LoopingMp3Player;

import javax.swing.*;
import java.io.IOException;
import java.nio.file.Path;

/**
 * JFrame注入GameJPanel，KeyboardListener
 */
public class MainJFrame extends JFrame {
    private ResourcesManager rm = new ResourcesManager("image");
    private OptionJPanel optionJPanel = new OptionJPanel(rm, 1, this);
    private indexJPanel indexJPanel = new indexJPanel(rm, this);
    private EditorJPanel editorJPanel = new EditorJPanel(rm, this);
    private GameJPanel gameJPanel;

    private LoopingMp3Player player = new LoopingMp3Player();

    public MainJFrame() throws IOException {
        this.setTitle("Tank War");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);

//        player.addBGM(1, "src/main/resources/bgm/Gaijin Entertainment,Jimmie Asche - Cloak and Dagger.wav");  //index
//        player.addBGM(2, "src/main/resources/bgm/Baltic Symphony Orchestra - Duty, Steel My Heart.mp3"); //编辑
//        player.addBGM(3, "src/main/resources/bgm/ChineseTech.mp3");  //选项
//        player.addBGM(4, "src/main/resources/bgm/Tim Wynn - Red Rock for Mother Russia.mp3");  //战斗

        player.addBGM(1, "/bgm/Gaijin Entertainment,Jimmie Asche - Cloak and Dagger.wav");  //index
        player.addBGM(2, "/bgm/Baltic Symphony Orchestra - Duty, Steel My Heart.mp3"); //编辑
        player.addBGM(3, "/bgm/ChineseTech.mp3");  //选项
        player.addBGM(4, "/bgm/Tim Wynn - Red Rock for Mother Russia.mp3");  //战斗

//        this.add(gameJPanel);
//        this.addKeyListener(keyboardListener);
//        gameJPanel.run();

//        this.add(new indexJPanel(new ResourcesManager("image")));

//        this.add(new OptionJPanel(new ResourcesManager("image"), 1));
//
//        this.pack();
        showIndex();

        this.setLocationRelativeTo(null);
        this.setVisible(true);

    }

    public void removeAll() {
        this.getContentPane().removeAll();
    }

    public void startGame(int playerNum, int diff, String mapFilePath) {
        removeAll();

        gameJPanel = new GameJPanel(rm, new ObjectManager(playerNum, diff, mapFilePath), this);

        add(gameJPanel);
        gameJPanel.setFocusable(true);
        gameJPanel.requestFocusInWindow();
        this.pack();

        gameJPanel.run();
        player.playBGM(4);
    }

    public void showOption(int state) throws IOException {
        removeAll();
        optionJPanel.playerNum = state;
        optionJPanel.loadMapNames(Path.of("map"));
        add(optionJPanel);
        this.pack();
        optionJPanel.requestFocusInWindow();
        optionJPanel.repaint();
        player.playBGM(3);
    }

    public void showIndex() {
        removeAll();
        add(indexJPanel);
        this.pack();
        indexJPanel.requestFocusInWindow();
        indexJPanel.repaint();
        player.playBGM(1);
    }

    public void showEditor() {
        removeAll();
        add(editorJPanel);
        this.pack();
        editorJPanel.requestFocusInWindow();
        editorJPanel.repaint();
        player.playBGM(2);
    }
}
